package com.zs4sdk.activity.resetPassword;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.common.library.data.CommonEventHandler;
import com.common.library.data.CommonHandle;
import com.common.library.data.EventHandler2Get;
import com.common.library.entity.JSONResultEntity;
import com.common.library.util.ActivityHelper;
import com.common.library.util.EncryptHelper;
import com.common.library.util.HandlerUtil;
import com.common.library.util.Helper;
import com.common.library.util.ParamsHelper;
import com.zs4sdk.activity.Zs4GameBaseActivity;
import com.zs4sdk.common.Extra;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/6/12.
 */

 class Zs4GameResetPassword {
    protected View mMainView;
    protected Zs4GameBaseActivity mActivity;
    //新密码输入框
    protected EditText mNewPasswordView;
    //新密码再次输入框
    protected EditText enSureNewPasswordView;

    //提交新密码
    protected Button mSubmitView;
    //提示
    protected TextView sunmitTipTextView;
    private String mobile;
    Zs4GameResetPassword(Zs4GameBaseActivity act , View view){
        mActivity = act;
        mMainView = view;
        getParams();
    }

    //获得参数
    public void getParams() {
        Intent intent = mActivity.getIntent();
        if (Helper.isNotNull(intent)) {
            if (intent.hasExtra("mobile")) {
                mobile = intent.getStringExtra("mobile");
            }else {
                ActivityHelper.showToast("参数错误，请返回重新验证手机号码！");
            }
        }
    }

    public void init(){

        mNewPasswordView = (EditText) findView(ActivityHelper.getIdResId("zs4gameviewid_mNewPasswordView_act_reset_password"));
        enSureNewPasswordView  = (EditText) findView(ActivityHelper.getIdResId("zs4gameviewid_mNewPasswordView_act_sure_reset_password"));

        mSubmitView = (Button) findView( ActivityHelper.getIdResId("zs4gameviewid_mSubmitView_act_reset_password"));
        sunmitTipTextView = (TextView)findView( ActivityHelper.getIdResId("zs4gameviewid_textview_retrivepassword_phone"));
        mSubmitView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cilcksubmit();
            }
        });
    }

    protected View findView(int id){
        return mMainView.findViewById(id);
    }
    //点击提交后的处理
    public void cilcksubmit() {
       if(isPasswordError()){

       } else {
           // httpGetForgetCodeNewPassword(mobile, newPassword);
            resetPassword();
        }
    }

    protected boolean isPasswordError(){
        String newPassword = mNewPasswordView.getText().toString().trim();
        String enSureNewPassword = enSureNewPasswordView.getText().toString().trim();
        if(newPassword.equals("")||enSureNewPassword.equals("")){
            sunmitTipTextView.setText(ActivityHelper.getStringResId("zs4gameNewPasswordNoNull"));
            return true;
        }

        if (newPassword.length() < 6 || newPassword.length() > 12) {
            //没有密码长度
            sunmitTipTextView.setText(ActivityHelper.getStringResId("zs4gamestring_hint_password_length"));
            return true;
        } else if (Helper.checkChinese(newPassword)) {
            // 密码为非中文
            sunmitTipTextView.setText(ActivityHelper.getStringResId("zs4gamestring_hint_password_unchinese"));
            return true;
        } else if(!newPassword.equals(enSureNewPassword)) {
            //两次密码不一样
            sunmitTipTextView.setText(ActivityHelper.getStringResId("zs4gamestring_hint_password_new_no_equals_affirm"));
            return true;
        }
        return false;
    }
    protected void resetPassword(){
        String newPassword = mNewPasswordView.getText().toString().trim();
        httpGetForgetCodeNewPassword(mobile, newPassword);
    }

    /**
     * 请求重置密码
     *
     * @param mobile
     * @param newPassword
     */
    private void httpGetForgetCodeNewPassword(final String mobile, final String newPassword) {

        HandlerUtil.sendMessage(myHandle, Extra.MessageKey.SHOWPROCESS);

        ArrayList<NameValuePair> paramList = new ArrayList<NameValuePair>();
        paramList.add(new BasicNameValuePair("v", Extra.VERSION_API));
        paramList.add(new BasicNameValuePair("mobile", EncryptHelper.encryptByAES(mobile)));
        paramList.add(new BasicNameValuePair("password", EncryptHelper.encryptByAES(EncryptHelper.md5(newPassword))));
//
        new EventHandler2Get(
                Extra.URL.RESET_PASSWORD,
                paramList,
                ParamsHelper.getUserAgent1(),
                new CommonEventHandler.ResultHandlerInEvent() {

                    @Override
                    public void doResultHanler(JSONObject result) throws JSONException {

                        HandlerUtil.sendMessage(myHandle, Extra.MessageKey.DISMISSPROCESS);

                        Message msg = myHandle.obtainMessage();

                        msg.what = Extra.MessageKey.REQUEST_SUCCESS;
                        msg.obj = result;

                        // 发送消息
                        myHandle.sendMessage(msg);

                    }

                    @Override
                    public void doExceptionHandler() {
                        HandlerUtil.sendMessage(myHandle, Extra.MessageKey.DISMISSPROCESS);
                    }
                });
    }


    CommonHandle myHandle = new CommonHandle(mActivity, new CommonHandle.HandleMessage() {

        @Override
        public void doHandleMessage(Message msg) {

            if (Extra.MessageKey.REQUEST_SUCCESS == msg.what) {
                JSONResultEntity entity = new JSONResultEntity((JSONObject) msg.obj);
                if (entity.getResult() == Extra.RESULTCODE.SUCCESS) {
                    ActivityHelper.showToast(ActivityHelper.getStringResId("zs4gamestring_hint_please_login_afterresetpassword"));
                    mActivity.finish();

                } else {
                    //请求失败提示
                    sunmitTipTextView.setText(entity.getDesc());
                }

            }

        }
    });

}
